<?php

namespace HospitalApi\Controller;

use Common\Util\LibUtil;
use Think\Controller;
//use Common\RestController;
use Common\CommonHospitalController;
use Common\Model\CarTurnoverModel;
use Common\Model\PatientModel;
use Common\Model\PatientLifeStatusModel;
use Common\Model\PatientNormalCureModel;
use Common\Model\CaseLogicModel;

class PatientController extends CommonHospitalController
{


    private $carTurnoverModel;
    private $patientModel;

    public function __construct()
    {
        parent::__construct();

        $this->carTurnoverModel = new CarTurnoverModel;
        $this->patientModel = new PatientModel;
    }


    /**
     * 获取病人信息
     * @type post
     * @param token , patient_id
     */
    public function getPatientInfo()
    {
        $patient_id = I('patient_id');
        if (empty($patient_id)) {
            $this->ajaxReturn(['status' => 0, 'patient_id' => '病人id必须填写']);
        }

        $info = $this->patientModel->getPatientInfo(I('patient_id'));
        if (empty($info)) {
            $this->ajaxReturn(['status' => 0, 'error_info' => '该病人不存在']);
        }
        $info['avatar_url'] = LibUtil::getPicUrl($info['image_id']);

        //返回病人信息
        $this->ajaxReturn(['status' => 1, 'data' => $info]);
    }

    /**
     * 修改病人信息
     * patient_id,
     * patient_info
     */
    public function editPatient()
    {
        $patient_id = I('patient_id');
        if (empty($patient_id)) {
            $this->ajaxReturn(['status' => 0, '接口传参错误，需要传patient_id']);
        }

        if (!$this->patientModel->create()) {
            $this->ajaxReturn(['status' => 0, 'error_info' => $this->patientModel->getError()]);
        }

        $this->patientModel->where(['id' => $patient_id])->save();
        $this->ajaxReturn(['status' => 1]);
    }

    /**
     * hospital端提交交接信息
     * token
     * car_id ,patient_id
     * signature 120用户签名
     */
    public function closeCase()
    {
        $caseLogicModel = new CaseLogicModel;
        $result = $caseLogicModel->closeCaseForHospital(I('patient_id'), I('signature'));
        $this->ajaxReturn($result);
    }

    /**
     * token
     * patient_id
     */
    public function getLastlifeStatus()
    {
        $lifeModel = new PatientLifeStatusModel;
        $record = $lifeModel->where(['patient_id' => I('patient_id')])->order('id desc')->limit(1)->select();
        if (empty($record)) {
            $this->ajaxReturn(['status' => 0, 'error_info' => '没有用户生命体征记录']);
        } else {
            $this->ajaxReturn(['status' => 1, 'data' => ['record' => $record]]);
        }
    }

    /**
     * token
     * patient_id
     * offset
     * length
     * all: 1 or 0
     */
    public function getlifestatus()
    {
        $all = I('all');
        $patient_id = I('patient_id');
        $lifeModel = new PatientLifeStatusModel;

        if ($all) {
            //all records
            $records = $lifeModel->getPatientAllRecords($patient_id);
        } else {
            //customer records
            $records = $lifeModel->getPatientRecords($patient_id, I('offset'), I("length"));
        }

        $count = $lifeModel->getPatientRecordsCount($patient_id);

        if (empty($records)) {
            $this->ajaxReturn(['status' => 0, 'error_info' => '没有用户生命体征记录']);
        } else {
            $this->ajaxReturn(['status' => 1, 'data' => ['record' => $records, 'count' => $count]]);
        }
    }

}